﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAOTicketReservationSystem;

namespace ManageEvents
{
    public partial class FormEvents : Form
    {
        private Dao dao;

        public FormEvents()
        {
            InitializeComponent();
        }

        private void FormEditEvents_Load(object sender, EventArgs e)
        {
            BindData();            
        }

        public void BindData()
        {
            dao = new Dao();
            eventBindingSource.DataSource = dao.GetAllEvents();
            categoryEventBindingSource.DataSource = dao.GetAllCategories();
            locationEventBindingSource.DataSource = dao.GetAllLocations();
            dataGridViewEvents.Rows[0].Selected = true;
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("You are going to delete selected event! Are you sure?", "Confirm delete",
                    MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Event currentEvent = (Event)dataGridViewEvents.SelectedRows[0].DataBoundItem;
                string deleteEventResult = dao.DeleteEventById(currentEvent.EventID);
                if (deleteEventResult == Dao.GetSuccessMessage())
                {
                    MessageBox.Show("Event deleted!", "Success");
                }
                else
                {
                    MessageBox.Show(deleteEventResult, "Error");
                }
                BindData();
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (dataGridViewEvents.SelectedRows.Count > 0)
            {
                Event currentEvent = (Event)dataGridViewEvents.SelectedRows[0].DataBoundItem;
                FormAddEditEvents newForm = new FormAddEditEvents();
                newForm.EventId = currentEvent.EventID;
                newForm.IsForAdd = false;
                newForm.IsForEdit = true;
                newForm.IsForDetails = false;
                newForm.ShowDialog();
                BindData();
            }
            else
            {
                MessageBox.Show("Select event to edit.");
            }
        }

        private void buttonAddEvent_Click(object sender, EventArgs e)
        {
            FormAddEditEvents newForm = new FormAddEditEvents();
            newForm.IsForAdd = true;
            newForm.IsForEdit = false;
            newForm.IsForDetails = false;
            newForm.ShowDialog();
            BindData();
        }

        private void buttonDetails_Click(object sender, EventArgs e)
        {
            if (dataGridViewEvents.SelectedRows.Count > 0)
            {
                Event currentEvent = (Event)dataGridViewEvents.SelectedRows[0].DataBoundItem;
                FormAddEditEvents newForm = new FormAddEditEvents();
                newForm.EventId = currentEvent.EventID;
                newForm.IsForAdd = false;
                newForm.IsForEdit = false;
                newForm.IsForDetails = true;
                newForm.ShowDialog();
                BindData();
            }
            else
            {
                MessageBox.Show("Select event to view details.");
            }
        }
    }
}
